using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Bll;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class RoleList : AdminBaseUserControl
    {
        public event EventHandler EventDoBindRole;
        public event EventHandler DisplayForm;
        public event EventHandler HiddenForm;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string RoleID
        {
            get { return (string)ViewState["roleID"]; }
            set { ViewState["roleID"] = value; }
        }

        public IList<IRole> DataSource
        {
            set { ViewState["_RoleList"] = value; }
        }

        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;
        }

        public void DoReLoadBind(object sender, EventArgs e)
        {
            long ClassID = Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]);
            if (ClassID > 0) BindRoles(ClassID);
            if (DisplayForm != null)
            {
                DisplayForm(this, e);
            }
        }
        public void DoBindGVRole(object sender, EventArgs e)
        {
            ClassList ucClassList = (ClassList)sender;
            long ClassID = Convert.ToInt64(ucClassList.SelectedValue);
            Session[SessionVariable.Clazz.CurrentID] = ClassID.ToString();
            BindRoles(ClassID);
            if (DisplayForm != null)
            {
                DisplayForm(this, e);
            }
        }

        private void BindRoles(long classID)
        {
            IList<IRole> roleList = RoleController.GetRoleList(classID);
            ICmsClass clazz = ClassController.GetClass(classID);
            lblStatus.Text = String.Format("Role list of class {0}", clazz.Name);
            if (roleList != null)
            {
                gvRole.DataSource = roleList;
                gvRole.DataBind();
            }

        }
        public void BindgvList()
        {
            if (Session[SessionVariable.Clazz.CurrentID] != null)
            {
                IList<IRole> roleList = RoleController.GetRoleList(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
                if (roleList != null)
                {
                    gvRole.DataSource = roleList;
                    gvRole.DataBind();
                }
            }
        }

        public string SelectedObjectValue
        {
            get { return (string)ViewState["_selectedRoleValue"]; }
            set { ViewState["_selectedRoleValue"] = value; }
        }

        protected void imgEdit_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton imgEdit = (ImageButton)sender;
            long ID = Convert.ToInt64(imgEdit.CommandArgument.ToString());
            SelectedObjectValue = ID.ToString();
            if (HiddenForm != null)
            {
                HiddenForm(this, new EventArgs());
            }
            if (EventDoBindRole != null)
            {
                EventDoBindRole(this, e);
            }
        }


        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton imgDelete = (ImageButton)sender;
            long ID = Convert.ToInt64(imgDelete.CommandArgument.ToString());
            RoleController.DeleteRole(ID);
            BindgvList();
        }
    }
}